app.controller('pvcountersign', function ($scope, $rootScope, $http, $interval, $compile, $state) {
    //详情页
    $scope.confirmsign = function(){
        $state.go(pvconfirmsign.stateName);
    }

    $scope.choose = function(index){
        if(1==index){  //选中 “现在签字”
            $scope.c = 1;
            $(".now").css("display","block");
            $(".shot").css("display","none");
        }else{ //拍照上传
            $scope.c = 2;
            $(".now").css("display","none");
            $(".shot").css("display","block");
        }
    }

    //拍照
    $scope.takePhoto = function () {
        if($scope.imageUrls.length >= 1){
            $rootScope.tipPopups.show(($rootScope.tipMessage = "最多上传1张图片"));
            return;
        }
        var scanType = "pvSignImageResult";
        if(checkPhone('android')){
            AndroidMethod.uploadImage(getToken(),scanType);
        }else if(checkPhone("iphone")){
            //context.uploadImage(getToken(),scanType);
			window.webkit.messageHandlers.uploadImage.postMessage({"scanType":scanType});
        }
    }

    //存入数组图片路径
    $scope.photoResult = function (url) {
        // alert(JSON.stringify(url));
        /*if(typeof(url)=='string'){
            $scope.imageUrls.push(JSON.parse(url).imageurl);
        }else{
            $scope.imageUrls.push(url.imageurl);
        }
        $scope.$applyAsync();*/

        if (navigator.onLine){ //正常工作
            if(typeof(url)=='string'){
                $scope.imageUrls.push(JSON.parse(url).imageurl);
            }else{
                $scope.imageUrls.push(url.imageurl);
            }
            $scope.$applyAsync();
        }else { //执行离线状态时的任务
            $rootScope.tipPopups.show(($scope.tipMessage = "当前网络不佳，稍后再试"));
            return;
        }
    }

    //删除图片
    $scope.removeImg = function (index) {
        $scope.imageUrls.splice(index,1);
        $scope.$applyAsync();
    }


    //获得数据
    $scope.submit = function () {
        if($scope.c==2){
            // alert(JSON.stringify($scope.imageUrls));
            if(!$scope.imageUrls || $scope.imageUrls.length==0){
                $rootScope.tipPopups.show(($rootScope.tipMessage = "请先拍照"));
                return false;
            }

            $scope.customerSignUrl = $scope.imageUrls[0];
            //存签名
            localStorage.setItem("pv-countcustomerSignUrl", $scope.customerSignUrl);

            $scope.confirmsign();
        }else {
            if ($("#signature").jSignature("getData", "native").length == 0) {
                $rootScope.tipPopups.show(($rootScope.tipMessage = "请先签字"));
                return false;
            }
            importSign('signature', 'signature', function (result) {
                if (!result.url) {
                    $rootScope.tipPopups.show(($rootScope.tipMessage = "请先签字"));
                    return;
                }

                if (1 == $scope.signType) {
                    $scope.customerSignUrl = result.url;
                    //存签名
                    localStorage.setItem("pv-countcustomerSignUrl", result.url);
                } else if (2 == $scope.signType) {
                    $scope.feSignUrl = result.url;
                    //存签名
                    localStorage.setItem("pv-countfeSignUrl", result.url);
                }
                $scope.confirmsign();
            });
        }
    }

    //初始化
    function init(){
        $scope.feSignUrl = "";
        $scope.customerSignUrl = "";

        $scope.c = "1";
        //客户签字的图片
        $scope.imageUrls = [];
        //签名类型
        $scope.signType = pvcountersign.params.type;
        $scope.signTypeName = "";
        /*if(1==$scope.signType){
            $scope.signTypeName = "客户";
        }else{
            $scope.signTypeName = "工程师";
        }*/

        if(1==$scope.signType){
            $scope.signTypeName = "客户";

            $(".choose").css("display","flex");
        }else{
            $scope.signTypeName = "工程师";

            $(".choose").css("display","none");
        }
    }

    init();


    //初始化画布
    $(document).ready(function(){
        var innnerheight = window.innerHeight;
        var height=(innnerheight - 250 )+'px';
        document.getElementById("sign").style.height = height;
        var width=100+'%';
        $("#signature").jSignature({height:height,width:width,color:"#000",lineWidth:4});
    })
    //重置画布
    $scope.clearSig = function(){
        $("#signature").jSignature("clear");
    }

    reloadJs();

});
//返回扫描结果
function pvSignImageResult(msg) {
    // alert(msg);
    var $scope = getScope("pvcountersign");
    $scope.photoResult(msg);
}